import { RequestConfig } from 'umi';
import { message } from 'antd';
import { useEffect } from 'react';
import './style/echarts.config';
// 路透变更
export function onRouteChange({ location, routes, action }: any) {}

// 拦截器
export const request: RequestConfig = {
  // prefix: 'http://www.baidu.com',
  timeout: 3000,
  middlewares: [],
  requestInterceptors: [
    function (url, config: any) {
      if (url !== '/api/pass/login') {
        const token = sessionStorage.getItem('token');
        console.log(sessionStorage.getItem('token'));
        config.headers['token'] = token;
      }
      return {
        url,
        config,
      };
    },
  ],
  responseInterceptors: [
    async function (response) {
      const codeMaps: any = {
        403: '没有权限',
        404: '请求资源未找到',
        502: '网关错误',
        503: '服务不可用,服务器暂时过载或维护',
        504: '网关超时',
      };

      if (response.status > 400) {
        message.error(codeMaps[response.status]);
      }

      return response;
    },
  ],

  // 错误处理
  errorHandler: function (error) {
    console.log(error);
    return { err_code: 1000, msg: '服务器异常' };
  },
};

// 笔记 运行时配置，可以类比于main.js
